package com.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.util.BODY;
import com.util.ConfigUtil;
import com.util.HttpRequest;
import com.util.JSONUtil;
import com.util.OpenKey;
import com.util.ReadConfig;
import com.util.SessionInfo;

/**
 * 缴存历史记录
 * @author 
 * 还没SQL语句 、功能以实现、账号用的是有数据的账号、等有SQL语句在改为当前登录人的账号
 */
@Controller
@RequestMapping("/jctsqrcontroller")
public class jctsqrcontroller {

	private static final Logger logger = Logger.getLogger(jctsqrcontroller.class);
	Map map  = new HashMap();
	List list = new ArrayList();
	  
	@RequestMapping("/test")
	@ResponseBody
	public String entryinterface(HttpServletRequest request,HttpSession session, HttpServletResponse response , String limit ,String id,String start,String end) throws IOException{
		SessionInfo sessionIn = (SessionInfo) session.getAttribute(ConfigUtil.getSessionInfoName());
		//读取配置文件路劲
		String interfaceUrl = ReadConfig.readValue();
		BODY b=new BODY();
		Map reqMap = new HashMap();
		OpenKey ope=new OpenKey();
		//接口编码
		reqMap.put("AUTHORITY", "YBGJJ132");
		//接口参数
		reqMap.put("LOGUserName","admin");
		reqMap.put("OpenKey",sessionIn.getKEY());
		b.setDWZH(sessionIn.getDWZH());
		reqMap.put("BODY",b);
		String result=ope.getOpenKey(reqMap, session);
		Map retMap = JSONUtil.toMap(result);
		logger.info("接口返回内容:"+result);
		if(result != null){
			Map resMap = JSONUtil.toMap(result);
				Map mx=JSONUtil.toMap(JSONUtil.ToJsongString2(resMap.get("body")));
				//map.put("count", resList.size());
				map.put("code", 0); 
				map.put("msg", resMap.get("RETURNDESC"));
				map.put("data", mx.get("RETDATA"));
				response.setHeader("Content-type", "text/html;charset=UTF-8");
				response.setCharacterEncoding("UTF-8");
				String jsonString = JSONUtil.ToJsongString2(map);
				PrintWriter pw = response.getWriter();
				pw.write(jsonString);
		}
		return null;
	}
	@RequestMapping("/test1")
	@ResponseBody
	public String entryinterface1(HttpSession session,HttpServletRequest request, HttpServletResponse response ,String limit ,String id,String start,String end,String YJNY) throws IOException{
		
		SessionInfo sessionIn = (SessionInfo) session.getAttribute(ConfigUtil.getSessionInfoName());
		//读取配置文件路劲
		System.out.println("======================="+YJNY);
		String interfaceUrl = ReadConfig.readValue();
		Map reqMap = new HashMap();
		BODY b=new BODY();
		OpenKey ope=new OpenKey();
		//接口编码
		reqMap.put("AUTHORITY", "YBGJJ133");
		//接口参数
		reqMap.put("LOGUserName","admin");
		reqMap.put("OpenKey",sessionIn.getKEY());
		b.setDWZH(sessionIn.getDWZH());
		reqMap.put("BODY",b);
		if(YJNY!=null && !YJNY.equals("")){
			b.setNIANYUE(YJNY);		
		}else{
			System.out.println("时间为空");
		}
		String resu=ope.getOpenKey(reqMap, session);
		 Map retMap = JSONUtil.toMap(resu);
		 if(retMap.get("RETURNCODE").equals("100")){
		
		 Map retMap1 = (Map) retMap.get("body");
		logger.info("接口返回内容:"+retMap);
		if(retMap1 != null){
				map.put("code", 0); 
				map.put("msg", retMap.get("RETURNDESC"));
				map.put("data", retMap1.get("RETDATA"));
				response.setHeader("Content-type", "text/html;charset=UTF-8");
				response.setCharacterEncoding("UTF-8");
				String jsonString = JSONUtil.ToJsongString2(map);
				PrintWriter pw = response.getWriter();
				pw.write(jsonString);
		}
		 
		 }
		return null;
	}
	
	@RequestMapping("/test2")
	@ResponseBody
	public String entryinterface2(HttpSession session,HttpServletRequest request, HttpServletResponse response ,String limit ,String YJJE,String NY) throws IOException{
		
		SessionInfo sessionIn = (SessionInfo) session.getAttribute(ConfigUtil.getSessionInfoName());
		//读取配置文件路劲
		String interfaceUrl = ReadConfig.readValue();
		Map reqMap = new HashMap();
		BODY b=new BODY();
		
		String[] s = NY.split(","); 
		String[] j = YJJE.split(",");  
		Map retMap=new HashMap();
		for(int i=0;i<s.length;i++){
			b.setDWZH(sessionIn.getDWZH());
			b.setNIANYUE(s[i]);
			b.setYJCE(j[i]);
			OpenKey ope=new OpenKey();
			//接口编码
			reqMap.put("AUTHORITY", "YBGJJ178");
			//接口参数
			reqMap.put("LOGUserName","admin");
			reqMap.put("OpenKey",sessionIn.getKEY());
			b.setDWZH(sessionIn.getDWZH());
			reqMap.put("BODY",b);
			String resu=ope.getOpenKey(reqMap, session);
			retMap = JSONUtil.toMap(resu);
			
		}
	
		
		
		
		 Map retMap1 = (Map) retMap.get("body");
		logger.info("接口返回内容:"+retMap);
		if(retMap1 != null){
				map.put("code", 0); 
				map.put("msg", retMap.get("RETURNDESC"));
				map.put("data", retMap1.get("RETDATA"));
				response.setHeader("Content-type", "text/html;charset=UTF-8");
				response.setCharacterEncoding("UTF-8");
				String jsonString = JSONUtil.ToJsongString2(map);
				PrintWriter pw = response.getWriter();
				pw.write(jsonString);
		}
		 
		return null;
	}
}
